//
//  XMN_GroupCell.m
//  B段项目
//
//  Created by dllo on 15/11/3.
//  Copyright © 2015年 谢美娜. All rights reserved.
//

#import "XMN_GroupCell.h"
#import "Header.h"

@implementation XMN_GroupCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _covImageView = [[UIImageView alloc]init];
        [self addSubview:_covImageView];
        
        _titleLabel = [[myTitleLabel alloc]init];
        [self addSubview:_titleLabel];
        
        _typeILView = [[XMN_ILView alloc]init];
        [self addSubview:_typeILView];
        
        _topView = [[UIView alloc]init];
        [self addSubview:_topView];
        
        _label1 = [[UILabel alloc]init];
        [_label1 NightWithType:UIViewColorType1];
        [self addSubview:_label1];
        _label2 = [[UILabel alloc]init];
        [_label2 NightWithType:UIViewColorType1];
        [self addSubview:_label2];
        _label3 = [[UILabel alloc]init];
        [_label3 NightWithType:UIViewColorType1];
        [self addSubview:_label3];
        _label4 = [[UILabel alloc]init];
        [_label4 NightWithType:UIViewColorType1];
        [self addSubview:_label4];
        
        _imageView1 = [[UIImageView alloc]init];
        [self addSubview:_imageView1];
        _imageView2 = [[UIImageView alloc]init];
        [self addSubview:_imageView2];
        _imageView3 = [[UIImageView alloc]init];
        [self addSubview:_imageView3];
        _imageView4 = [[UIImageView alloc]init];
        [self addSubview:_imageView4];
        
        _tLabel1 = [[UILabel alloc]initWithFrame:CGRectMake(5 * FitWidth, 10 * FitHeight, 50 * FitWidth, 15 * FitHeight)];
        [_imageView1 addSubview:_tLabel1];
        _tLabel1.font = [UIFont systemFontOfSize:12];
        _tLabel1.textColor = [UIColor whiteColor];
        _cLabel1 = [[UILabel alloc]initWithFrame:CGRectMake(_tLabel1.frame.origin.x, _tLabel1.frame.origin.y + _tLabel1.frame.size.height + 5 * FitHeight, _tLabel1.frame.size.width, 25 * FitHeight)];
        _cLabel1.numberOfLines = 0;
        [_imageView1 addSubview:_cLabel1];
        _cLabel1.font = [UIFont systemFontOfSize:10];
        _cLabel1.textColor = [UIColor grayColor];
        
        _tLabel2 = [[UILabel alloc]initWithFrame:CGRectMake(5 * FitWidth, 10 * FitHeight, 50 * FitWidth, 15 * FitHeight)];
        [_imageView2 addSubview:_tLabel2];
        _tLabel2.font = [UIFont systemFontOfSize:12];
        _tLabel2.textColor = [UIColor whiteColor];
        _cLabel2 = [[UILabel alloc]initWithFrame:CGRectMake(_tLabel2.frame.origin.x, _tLabel2.frame.origin.y + _tLabel2.frame.size.height + 5 * FitHeight, _tLabel2.frame.size.width, 25 * FitHeight)];
        [_imageView2 addSubview:_cLabel2];
        _cLabel2.numberOfLines = 0;
        _cLabel2.font = [UIFont systemFontOfSize:10];
        _cLabel2.textColor = [UIColor grayColor];
        
        _tLabel3 = [[UILabel alloc]initWithFrame:CGRectMake(5 * FitWidth, 10 * FitHeight, 50 * FitWidth, 15 * FitHeight)];
        [_imageView3 addSubview:_tLabel3];
        _tLabel3.font = [UIFont systemFontOfSize:12];
        _tLabel3.textColor = [UIColor whiteColor];
        _cLabel3 = [[UILabel alloc]initWithFrame:CGRectMake(_tLabel3.frame.origin.x, _tLabel3.frame.origin.y + _tLabel3.frame.size.height + 5 * FitHeight, _tLabel3.frame.size.width, 25 * FitHeight)];
        [_imageView3 addSubview:_cLabel3];
        _cLabel3.numberOfLines = 0;
        _cLabel3.font = [UIFont systemFontOfSize:10];
        _cLabel3.textColor = [UIColor grayColor];
        
        _tLabel4 = [[UILabel alloc]initWithFrame:CGRectMake(5 * FitWidth, 10 * FitHeight, 50 * FitWidth, 15 * FitHeight)];
        [_imageView4 addSubview:_tLabel4];
        _tLabel4.font = [UIFont systemFontOfSize:12];
        _tLabel4.textColor = [UIColor whiteColor];
        _cLabel4 = [[UILabel alloc]initWithFrame:CGRectMake(_tLabel4.frame.origin.x, _tLabel4.frame.origin.y + _tLabel4.frame.size.height + 5 * FitHeight, _tLabel4.frame.size.width, 25 * FitHeight)];
        [_imageView4 addSubview:_cLabel4];
        _cLabel4.numberOfLines = 0;
        _cLabel4.font = [UIFont systemFontOfSize:10];
        _cLabel4.textColor = [UIColor grayColor];
        
        _menLabel = [[UILabel alloc]init];
        _menLabel.font = [UIFont systemFontOfSize:11];
        _menLabel.textColor = [UIColor lightGrayColor];
        _menLabel.textAlignment = 2;
        [self addSubview:_menLabel];
        
        _fromLabel = [[UILabel alloc]init];
        _fromLabel.font = [UIFont systemFontOfSize:13];
        _fromLabel.textColor = [UIColor lightGrayColor];
        [self addSubview:_fromLabel];
        
        _label1.layer.masksToBounds = YES;
        _imageView1.layer.masksToBounds = YES;
        _label1.layer.cornerRadius = 5;
        _imageView1.layer.cornerRadius = 5;
        
        _label2.layer.masksToBounds = YES;
        _imageView2.layer.masksToBounds = YES;
        _label2.layer.cornerRadius = 5;
        _imageView2.layer.cornerRadius = 5;
        
        _label3.layer.masksToBounds = YES;
        _imageView3.layer.masksToBounds = YES;
        _label3.layer.cornerRadius = 5;
        _imageView3.layer.cornerRadius = 5;
        
        _label4.layer.masksToBounds = YES;
        _imageView4.layer.masksToBounds = YES;
        _label4.layer.cornerRadius = 5;
        _imageView4.layer.cornerRadius = 5;
        
        _imageView1.userInteractionEnabled = YES;
        _imageView2.userInteractionEnabled = YES;
        _imageView3.userInteractionEnabled = YES;
        _imageView4.userInteractionEnabled = YES;
        
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    _topView.frame = CGRectMake(0, 0, ScreenWidth, 60 * FitHeight);
    _covImageView.frame = CGRectMake(20 * FitWidth, 10 * FitHeight, 40 * FitWidth, 40 * FitHeight);
    _covImageView.layer.masksToBounds = YES;
    _covImageView.layer.cornerRadius = 20 * FitWidth;
    _titleLabel.frame = CGRectMake(_covImageView.frame.origin.x + _covImageView.frame.size.width + 10 * FitWidth, _covImageView.frame.origin.y - 2 * FitHeight, 200 * FitWidth, 20 * FitHeight);
    _typeILView.frame = CGRectMake(_titleLabel.frame.origin.x, _titleLabel.frame.origin.y + _titleLabel.frame.size.height + 5 * FitHeight, 180 * FitWidth, 20 * FitHeight);
    _menLabel.frame = CGRectMake(270 * FitWidth, _typeILView.frame.origin.y + 5 * FitHeight, 90 * FitWidth, 15 * FitHeight);
    
    _imageView1.frame = CGRectMake(_covImageView.frame.origin.x, _covImageView.frame.origin.y + _covImageView.frame.size.height + 10 * FitHeight, 60 * FitWidth, 60 * FitHeight);
    _imageView2.frame = CGRectMake(_imageView1.frame.origin.x + _imageView1.frame.size.width + 20 * FitWidth, _imageView1.frame.origin.y, _imageView1.frame.size.width, _imageView1.frame.size.height);
    _imageView3.frame = CGRectMake(_imageView2.frame.origin.x + _imageView2.frame.size.width + 20 * FitWidth, _imageView2.frame.origin.y, _imageView2.frame.size.width, _imageView2.frame.size.height);
    _imageView4.frame = CGRectMake(_imageView3.frame.origin.x + _imageView3.frame.size.width + 20 * FitWidth, _imageView3.frame.origin.y, _imageView3.frame.size.width, _imageView3.frame.size.height);
    _label1.frame = _imageView1.frame;
    _label2.frame = _imageView2.frame;
    _label3.frame = _imageView3.frame;
    _label4.frame = _imageView4.frame;
    
    _fromLabel.frame = CGRectMake(_covImageView.frame.origin.x, self.contentView.frame.size.height - 25 * FitHeight, 150 * FitWidth, 15 * FitHeight);
    
    UITapGestureRecognizer *viewTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(viewTapAction)];
    [_topView addGestureRecognizer:viewTap];
    
    UITapGestureRecognizer *imageView1Tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageView1TapAction)];
    [_imageView1 addGestureRecognizer:imageView1Tap];
    
    UITapGestureRecognizer *imageView2Tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageView2TapAction)];
    [_imageView2 addGestureRecognizer:imageView2Tap];
    
    UITapGestureRecognizer *imageView3Tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageView3TapAction)];
    [_imageView3 addGestureRecognizer:imageView3Tap];
    
    UITapGestureRecognizer *imageView4Tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageView4TapAction)];
    [_imageView4 addGestureRecognizer:imageView4Tap];
}

-(void)setGroup:(XMN_Group *)group{
    [_covImageView sd_setImageWithURL:[NSURL URLWithString:group.coverimg]];
    [_covImageView sd_setImageWithURL:[NSURL URLWithString:group.coverimg] placeholderImage:[UIImage imageNamed:@"XMN_Shequ_Cell"]];
    _titleLabel.text = group.title;
    switch (group.tags.count) {
        case 0:{
            _typeILView.rightLabel.text = @"未分类";
            break;
        }
        case 1:{
            _typeILView.rightLabel.text = [NSString stringWithFormat:@"%@", [group.tags objectAtIndex:0]];
            break;
        }
        case 2:{
            _typeILView.rightLabel.text = [NSString stringWithFormat:@"%@/%@", [group.tags objectAtIndex:0], [group.tags objectAtIndex:1]];
            break;
        }
        case 3:{
            _typeILView.rightLabel.text = [NSString stringWithFormat:@"%@/%@/%@", [group.tags objectAtIndex:0], [group.tags objectAtIndex:1], [group.tags objectAtIndex:2]];
            break;
        }
    }
    _typeILView.leftImageView.image = [UIImage imageNamed:@"XMN_type"];
    _menLabel.text = [NSString stringWithFormat:@"%ld 人加入", group.membernum];

    
    [_imageView1 sd_setImageWithURL:[NSURL URLWithString:[[group.latestposts objectAtIndex:0] objectForKey:@"coverimg"]]];
    [_imageView2 sd_setImageWithURL:[NSURL URLWithString:[[group.latestposts objectAtIndex:1] objectForKey:@"coverimg"]]];
    [_imageView3 sd_setImageWithURL:[NSURL URLWithString:[[group.latestposts objectAtIndex:2] objectForKey:@"coverimg"]]];
    [_imageView4 sd_setImageWithURL:[NSURL URLWithString:[[group.latestposts objectAtIndex:3] objectForKey:@"coverimg"]]];
    
    
    _tLabel1.text = [[group.latestposts objectAtIndex:0] objectForKey:@"title"];
    _cLabel1.text = [[group.latestposts objectAtIndex:0] objectForKey:@"content"];
    
    _tLabel2.text = [[group.latestposts objectAtIndex:1] objectForKey:@"title"];
    _cLabel2.text = [[group.latestposts objectAtIndex:1] objectForKey:@"content"];
    
    _tLabel3.text = [[group.latestposts objectAtIndex:2] objectForKey:@"title"];
    _cLabel3.text = [[group.latestposts objectAtIndex:2] objectForKey:@"content"];
    
    _tLabel4.text = [[group.latestposts objectAtIndex:3] objectForKey:@"title"];
    _cLabel4.text = [[group.latestposts objectAtIndex:3] objectForKey:@"content"];
    
    _fromLabel.text = [NSString stringWithFormat:@"组长: %@", [group.userinfo objectForKey:@"uname"]];
}

#pragma mark - 手势相应事件
-(void)viewTapAction{
    [self.delegate delegatePushAction:_indexPath];
}

-(void)imageView1TapAction{
    [self.delegate delegateImageViewPushAction:_indexPath :0];
}

-(void)imageView2TapAction{
    [self.delegate delegateImageViewPushAction:_indexPath :1];
}

-(void)imageView3TapAction{
    [self.delegate delegateImageViewPushAction:_indexPath :2];
}

-(void)imageView4TapAction{
    [self.delegate delegateImageViewPushAction:_indexPath :3];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
